package controllers;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import pojo.User;


//RestController
@Controller
public class UserController {
	@RequestMapping(value="user")
	public String findAllUsers(@RequestParam(value="uid") int id,String uname,boolean sex){
		System.out.println("find allusers...."+(id+1)+"..."+uname+"..."+sex);
		return "success";
	}
	
	@RequestMapping(value="user1")
	public String findAllUsers1(User u){
		System.out.println("find allusers...."+u.getId()+"..."+u.getUname()+"..."+u.isSex());
		return "success";
	}
	
	@RequestMapping(value="user2")
	public String findAllUsers1(@RequestParam Map<String, String> mp){
		System.out.println("find allusers...."+mp.get("uname"));
		return "success";
	}
	
	@RequestMapping(value="user3")
	public String findAllUsers1(@CookieValue(value="JSESSIONID") String sid){
		System.out.println("find allusers...."+sid);
		return "success";
	}
	
	@RequestMapping(value="user4/{id}/book/{bid}")
	public String findAllUsers4(@PathVariable(value="id") int uid,@PathVariable String bid){
		System.out.println("find allusers...."+uid+"...."+bid);
		return "success";
	}
	
	
	@RequestMapping(value="user6")
	public String findAllUsers4(HttpSession sen,HttpServletRequest requset){
		sen.setAttribute("uname", "johnyu good!!!");
		return "success";
	}
	
	@RequestMapping(value="user7")
	public String findAllUsers4(Model model){
		model.addAttribute("uname", "JohnYu!!!");
		return "success";
	}
	
	@RequestMapping(value="user8")
	public String findAllUsers8(Model model){
		model.addAttribute("uname", "JohnYu!!!");
		return "redirect:success.jsp";
	}
	@RequestMapping(value="getPic")
	public void  getPic(OutputStream out,HttpSession sen) throws Exception{
		String path=sen.getServletContext().getRealPath("/images/bubble.png");
		System.out.println(path);
		
		InputStream in=new FileInputStream("/Users/john/bubble.png");
		byte[] bs=new byte[in.available()];
		in.read(bs);
		out.write(bs);
		in.close();
		out.close();
	}
	@ResponseBody
	@RequestMapping(value="user9")
	public User findAllUsers8(@RequestBody User u){
		//User u=new User();
		//u.setId(120);u.setUname("johnyu");u.setSex(true);
		return u;
	}
	@ResponseBody
	@RequestMapping(value="down")
	public byte[] download(HttpServletResponse resp) throws Exception{
		InputStream in=new FileInputStream("/Users/john/bubble.png");
		byte[] buf=new byte[in.available()];
		
		in.read(buf);
		in.close();
		resp.addHeader("Content-Disposition", "attachment;filename=xx.png");
		return buf;
	}
	
	
	
}
